﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmTreeView01 : Form
    {
        public FrmTreeView01()
        {
            InitializeComponent();
        }

     
        private void btnExpand_Click(object sender, EventArgs e)
        {
            treeView1.ExpandAll();//展开所有节点
        }

        private void btnCollaspe_Click(object sender, EventArgs e)
        {
            treeView1.CollapseAll();//折叠所有节点
        }

        private void btnAddNodes_Click(object sender, EventArgs e)
        {
            TreeNode node = new TreeNode();
            node.Name = "StudentNode";
            node.Text = "学生管理";
            treeView1.Nodes.Add(node);//添加一个根节点

            //添加子节点
            TreeNode subNode1 = new TreeNode();
            subNode1.Name = "sub01";
            subNode1.Text = "新增学生";
            node.Nodes.Add(subNode1);//添加一个子节点
            TreeNode subNode2 = new TreeNode();
            subNode2.Name = "sub02";
            subNode2.Text = "学生列表";
            node.Nodes.Add(subNode2);//添加一个子节点

            TreeNode subNode3 = new TreeNode();
            subNode3.Name = "sub03";
            subNode3.Text = "学生查询";
            subNode2.Nodes.Add(subNode3);//添加一个三级节点
           //如果还有，再继续添加
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            //访问第二个根节点
            TreeNode rootNode2=  treeView1.Nodes[1];
            TreeNode rootNode1 = rootNode2.PrevNode;//访问同级的上一个节点
            label1.Text = "同级上一节点：" + rootNode1.Text + "\n";
            TreeNode rootNode3 = rootNode2.NextNode;//访问同级的下一个节点
            label1.Text += "同级下一节点：" + rootNode3.Text + "\n";
            TreeNode subNode = rootNode3.FirstNode;//访问第一个子节点
            label1.Text += "第一个子节点：" + subNode.Text + "\n";
            TreeNode subNode2 = rootNode3.LastNode;//访问最后一个子节点
            label1.Text += "最后一个子节点：" + subNode2.Text + "\n";
            TreeNode pNode = subNode2.Parent;//访问该节点的父节点
            label1.Text += "父节点：" + pNode.Text + "\n";
            TreeNode childNode = rootNode2.Nodes[0];//访问子节点
            rootNode3.Expand();
            //rootNode3.ExpandAll();//展开该所有子节点
            //rootNode2.Collapse();//折叠该节点
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;//获取操作的节点
            //处理子节点的勾选状态
           
            if(e.Action==TreeViewAction.ByMouse|| e.Action == TreeViewAction.ByKeyboard)
            {
                foreach (TreeNode child in node.Nodes)
                {
                    child.Checked = node.Checked;
                }
                TreeNode pNode = node.Parent;
                if (pNode != null)
                {
                    pNode.Checked = node.Checked;//不完全对????  讲父子节点勾选时再来讲
                }
            }
            
        }

        /// <summary>
        /// 选定节点后发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string moduleName = e.Node.Text;//获取当前选定节点的文本
            //获取名称做后续的事......   类别名称 -----》信息查询操作。。。。
        }

        /// <summary>
        /// 展开有子节点的节点才会引发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            MessageBox.Show("展开："+e.Node.Text);
        }

        /// <summary>
        /// 折叠有子节点的节点才会引发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            MessageBox.Show("折叠：" + e.Node.Text);
        }

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            TreeNode node = e.Item as TreeNode;
            DoDragDrop(node, DragDropEffects.Move);//开始拖动
        }

        private void treeView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void treeView1_DragOver(object sender, DragEventArgs e)
        {
            TreeView tv = sender as TreeView;
            tv.SelectedNode = tv.GetNodeAt(tv.PointToClient(new Point(e.X, e.Y)));
        }

        //拖放结束后处理
        private void treeView1_DragDrop(object sender, DragEventArgs e)
        {
            TreeView tv = sender as TreeView;
            TreeNode dragNode = e.Data.GetData(typeof(TreeNode)) as TreeNode;
            if(dragNode.Equals(tv.SelectedNode))
            {
                return;
            }
            if(e.Effect==DragDropEffects.Move)
            {
                if(tv.SelectedNode==null)
                {
                    tv.Nodes.Add(dragNode.Clone() as TreeNode);//临时节点
                    dragNode.Remove();
                    return;
                }
                dragNode.Remove();
                tv.SelectedNode.Nodes.Add(dragNode);
            }
            dragNode.Expand();
        }
    }
}
